<script setup lang="ts">
import { computed, inject, onBeforeUnmount } from 'vue';
import AsaiWeb from './AsaiWeb.vue';
import AsaiWebServer from './AsaiWebServer.vue';
import AsaiWebServerUser from './AsaiWebServerUser.vue';

const props: any = defineProps<{
    appmodules: any;
    ujt: any;
}>();

const $vueProxy: any = inject('$vueProxy');

// 记录当前不设防的公共路由
const { $global, $fn, $model } = props.ujt;

// 挂载app传来的选项值
$model.appmodules = props.appmodules;

// 初始化前端服务的数据与全局变量
$fn.initweb($vueProxy).then(() => {
    // 初始化API默认连接状态
    $fn.useApi(props.ujt, 'webserver');
    // 初始化WS默认连接状态
    $fn.useWs(props.ujt, 'web');
    let newpros: any = [
        // new Promise((resolve: any, reject: any) => {
        //     $fn.ws['web'].init().finally(() => {
        //         resolve();
        //     });
        // }),
    ];
    if (!$global?.sys?.local?.lang) {
        newpros.push(
            $fn.api['webserver']
                .apiGet(
                    '/api/asaifile/manage/once/?path=./webclient/webmodel/sys/setting/settinglang.json'
                )
                .then((res: any) => {
                    if (typeof res?.data === 'string') {
                        $global.config.langcur = JSON.parse(
                            res?.data || '{}'
                        )?.lang;
                    } else {
                        $global.config.langcur = (res?.data || {})?.lang;
                    }
                })
        );
    }
    Promise.all(newpros).then(() => {
        // 初始化语言包
        $fn.changelang($global.config.langcur).then((ress) => {
            // 赋值当前网页标题
            $global.lang.sys.webtit &&
                (document.title = $global.lang.sys.webtit);
            // 前端public数据成功加载的标识
            $global.stat.webclient = 1;
        });
    });
});

// 判断当前路由访问等级
const cmRouterRight: any = computed(() => {
    if (
        !$global.location.hash ||
        $global.location.hash == '/' ||
        $global.sys?.router?.rights?.lv0?.find((el: string) =>
            $global.location.hash.includes(el)
        )
    ) {
        return 0;
    } else if (
        $global.sys?.router?.rights?.lv1?.find((el: string) =>
            $global.location.hash.includes(el)
        )
    ) {
        return 1;
    } else if (
        $global.sys?.router?.rights?.lv2?.find((el: string) =>
            $global.location.hash.includes(el)
        )
    ) {
        return 2;
    } else if (
        $global.sys?.router?.rights?.lv3?.find((el: string) =>
            $global.location.hash.includes(el)
        )
    ) {
        return 3;
    }
    return $global.sys?.router?.config?.lv;
});
onBeforeUnmount(() => {
    $fn.ws['web'].wsWatchOff({
        ty: 'publish/web',
    });
});
</script>

<template>
    <template v-if="$global.stat.webclient">
        <AsaiWeb v-if="cmRouterRight === 0" :ujt="ujt"></AsaiWeb>
        <AsaiWebServer
            v-else-if="cmRouterRight === 1"
            :ujt="ujt"
        ></AsaiWebServer>
        <AsaiWebServerUser
            v-else-if="cmRouterRight === 2"
            :ujt="ujt"
        ></AsaiWebServerUser>
        <AsaiWebServerUserOnline v-else :ujt="ujt"></AsaiWebServerUserOnline>
    </template>
    <component
        v-else
        :ujt="ujt"
        :is="$global.sys?.router?.config?.loading || 'PageLoadClient'"
    ></component>
    <div id="popteleport"></div>
    <PopMsg :ujt="ujt"></PopMsg>
    <PopBoxConfirm :ujt="ujt" id="popdefaultconfirm"></PopBoxConfirm>
</template>

<style lang="scss"></style>
